<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="文件名称">
              <a-input placeholder="请输入文件名称" v-model="queryParam.docName"></a-input>
<!--              <a-input placeholder="请输入文件名称" v-model="docName"></a-input>-->
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="三甲条款">
              <a-input placeholder="请输入三甲条款" v-model="queryParam.jic"></a-input>
<!--              <a-input placeholder="请输入三甲条款" v-model="jic"></a-input>-->
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="文档状态">
                <j-dict-select-tag placeholder="请选择文档状态" v-model="queryParam.docStatus" dictCode="doc_status"/>
<!--                <j-dict-select-tag placeholder="请选择文档状态" v-model="docStatus" dictCode="doc_status"/>-->
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否过期">
                <j-dict-select-tag placeholder="请选择是否过期" v-model="queryParam.docExpired" dictCode="doc_expired"/>
<!--                <j-dict-select-tag placeholder="请选择是否过期" v-model="docExpired" dictCode="doc_expired"/>-->
              </a-form-item>
            </a-col>
          </template>
        </a-row>
        <a-row :gutter="24">
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="生效日期">
                <j-date placeholder="开始日期" class="query-group-cust" v-model="queryParam.sxTime_begin"></j-date>
                <j-date placeholder="结束日期" class="query-group-cust" v-model="queryParam.sxTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="到期日期">
                <j-date placeholder="开始日期" class="query-group-cust" v-model="queryParam.dqTime_begin"></j-date>
                <j-date placeholder="结束日期" class="query-group-cust" v-model="queryParam.dqTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="创建日期">
                <j-date placeholder="开始日期" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
                <j-date placeholder="结束日期" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="更新日期">
                <j-date placeholder="开始日期" class="query-group-cust" v-model="queryParam.updateTime_begin"></j-date>
                <j-date placeholder="结束日期" class="query-group-cust" v-model="queryParam.updateTime_end"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
<!--              <a-button type="primary" @click="loadCheck(1)" icon="search">查询</a-button>-->
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <template>
      <a-row :gutter="10">
        <a-col :md="4" :sm="24">
          <a-card :bordered="false">
            <div style="background: #fff;padding-left:1px;height: 100%; margin-top: 5px" class="no-left">
              <!-- 树-->
              <template v-if="userIdentity === '2' && categoryTree.length>0">
                <!--组织机构-->
                <a-tree
                  showLine
                  :checkStrictly="true"
                  :treeData="categoryTree"
                  :autoExpandParent="autoExpandParent"
                  @select="onSelect"
                />
              </template>
              <template v-else-if="userIdentity === '2' && categoryTree.length==0">
                <a-empty/>
              </template>
            </div>
          </a-card>
        </a-col>
        <a-col :md="20" :sm="24">
          <a-card :bordered="false">
            <div class="table-operator">
              <a-button type="primary" icon="search" @click="batchRecord()">操作记录</a-button>
              <a-button type="primary" icon="download" @click="handleDownloads">批量下载</a-button>
              <a-dropdown v-if="selectedRowKeys.length > 0">
                <a-menu slot="overlay">
                  <a-menu-item key="1" @click="batchDel">
                    <a-icon type="delete"/>
                    删除
                  </a-menu-item>
                  <a-menu-item key="2" @click="batchStartDate">
                    <a-icon type="menu-fold"/>
                    修改生效日期
                  </a-menu-item>
                  <a-menu-item key="3" @click="batchEndDate">
                    <a-icon type="menu-fold"/>
                    修改到期日期
                  </a-menu-item>
                </a-menu>
                <a-button style="margin-left: 8px"> 批量操作
                  <a-icon type="down"/>
                </a-button>
              </a-dropdown>
            </div>

            <!-- table区域-begin -->
            <div>
              <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
                <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
                selectedRowKeys.length }}</a>项
                <a style="margin-left: 24px" @click="onClearSelected">清空</a>
              </div>

              <a-table
                ref="table"
                size="middle"
                bordered
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                :rowSelection="{fixed:true, selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                @change="handleTableChange">
          <span slot="action" slot-scope="text, record">
            <a v-if="record.download==0">
                  <a @click="handleDoload(record)">下载</a>
            </a>
            <a v-if="record.download!=0">
                  <a disabled="true">下载</a>
            </a>
            <a-divider type="vertical"/>
            <a-dropdown>
              <a class="ant-dropdown-link">更多<a-icon type="down"/></a>
              <a-menu slot="overlay">
                <a-menu-item>
                 <a @click="handleRecord(record)">操作记录</a>
                </a-menu-item>
                <a-menu-item>
                  <a @click="batchStartDate(record,1)">修改生效日期</a>
                </a-menu-item>
                <a-menu-item>
                  <a @click="batchEndDate(record,1)">修改到期日期</a>
                </a-menu-item>
            </a-menu>
            </a-dropdown>
          </span>
              </a-table>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </template>

    <!--    <doc-modal ref="modalForm" @ok="modalFormOk"/>-->
    <RecordModal ref="recordForm"/>
    <!--    <DateModal ref="dateForm" :selectedRowKeys="selectedRowKeys" @ok="this.handleLoadData"></DateModal>-->
    <DateModal ref="dateForm" :selectedRowKeys="selectedRowKeys" @ok="this.sx"></DateModal>
  </a-card>
</template>

<script>

  import {JeecgListMixin} from '@/mixins/JeecgListMixin'

  import RecordModal from './modules/RecordModal'
  import DateModal from './modules/DateModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import {queryCategoryTreeList} from '@/api/api'
  import {getAction,postAction,handleExportDoc} from '@/api/manage'
  import {axios} from '../../utils/request'
  import {frozenCategory} from '../../api/api'
  import {frozenDoc} from '../../api/api'
  import {handleExportDocs} from '../../api/manage'
  import JDate from "../../components/jeecg/JDate";

  export default {
    name: 'DocManager',
    mixins: [JeecgListMixin],
    components: {
      DateModal,
      JDictSelectTag,
      RecordModal,
      JDate
    },
    // created() {
    //   this.refresh()
    // },
    data() {
      return {
        jic: "",
        docName: "",
        docExpired: "",
        docStatus: "",
        startDate: {},
        endDate: {},
        selectId: [],
        ids: [],
        docNames: [],
        id: '',
        selectedKeys: [],
        dataSource: [],
        visible: false,
        hiding: true,
        disableSubmit: false,
        iExpandedKeys: [],
        loading: false,
        autoExpandParent: true,
        disable: true,
        treeData: [],
        categoryTree: [],
        userIdentity: '',
        description: '文档管理页面',
        event: '',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            /*fixed: 'left',*/
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            }
          },
          // {// 根据sort字段排序
          //   sortOrder: 'ascend',
          //   sorter: (a, b) => a.sort - b.sort,
          // },
          {
            title: '文件分类',
            align: 'center',
            /*width: 100,*/
            /*fixed: 'left',*/
            dataIndex: 'categoryName'
          },
          {
            title: '文件名称',
            align: 'center',
            /*width: 100,*/
            /*fixed: 'left',*/
            dataIndex: 'docName'
          },
          {
            title: '文件编号',
            align: 'center',
            /*width: 100,*/
            dataIndex: 'docNo'
          },
          {
            title: '定制科室',
            align: 'center',
            /*width: 100,*/
            dataIndex: 'dzDepartment'
          },
          // {
          //   title: '文件大小',
          //   align: 'center',
          //   width: 100,
          //   dataIndex: 'docSize'
          // },
          {
            title: '三甲条款',
            align: 'center',
            /*width: 100,*/
            dataIndex: 'jic'
          },
          {
            title: '生效日期',
            align: 'center',
            /*width: 120,*/
            dataIndex: 'sxTime',
            customRender: function (text) {
              return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          // {
          //   title: '到期日期',
          //   align: 'center',
          //   width: 100,
          //   dataIndex: 'dqTime',
          //   customRender: function (text) {
          //     return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          //   }
          // },
          // {
          //   title: '是否三甲条款',
          //   align: 'center',
          //   /*width: 100,*/
          //   dataIndex: 'isJic_dictText'
          // },
          // {
          //   title: '能否下载',
          //   align: 'center',
          //   width: 100,
          //   dataIndex: 'download_dictText'
          // },
          // {
          //   title: '能否打印',
          //   align: 'center',
          //   width: 100,
          //   dataIndex: 'print_dictText'
          // },
          {
            title: '审核人',
            align: 'center',
            /*width: 100,*/
            dataIndex: 'audit'
          },
          // {
          //   title: '富文本',
          //   width: 100,
          //   align: "center",
          //   dataIndex: 'editorString'
          // },
          {
            title: '文档状态',
            /*width: 100,*/
            align: 'center',
            dataIndex: 'docStatus_dictText'
          },
          // {
          //   title: '是否过期',
          //   width: 100,
          //   align: 'center',
          //   dataIndex: 'docExpired_dictText'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: 155,
            /*fixed: 'right',*/
            scopedSlots: {customRender: 'action'}
          }
        ],
        url: {
          list: '/doc/doc/list',
          doLoads: '/doc/doc/doLoads',
          doLoad: '/doc/doc/doLoad',
          delete: '/doc/doc/delete',
          deleteBatch: '/doc/doc/deleteBatch',
          exportXlsUrl: '/doc/doc/exportXls',
          importExcelUrl: 'doc/doc/importExcel',
        },
        dictOptions: {},
      }
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
      ,
    }
    ,
    methods: {
      rangeQueryDate() {

      },
      onChange(date) {
        this.startDate = date[0]._d
        this.endDate = date[1]._d
      },
      /*handleDownload: function () {
        this.docNames = []
        this.selectId = []
        for (let i = 0; i < this.selectedRowKeys.length; i++) {
          let selectedRowKey = this.selectedRowKeys[i]
          if (selectedRowKey == 0) {
            this.selectId.push(selectedRowKey)
          }
          for (let j = 0; j < this.dataSource.length; j++) {
            let dataSourceElement = this.dataSource[j]
            if (dataSourceElement['id'] == selectedRowKey && dataSourceElement['download'] == 0) {
              this.docNames.push(dataSourceElement['docName'])
            }
          }
        }
        handleExportDocs(this.docNames, this.selectId)
      },*/
      handleDoload(record){
        var param={
          docName:record.docName,
          id:record.id
        }
        getAction(this.url.doLoad, param).then((res) => {
          window.open(window._CONFIG['domianURL']+"/"+res.result.fileKk);
        })

      },
      handleDownloads: function () {
        this.selectId = []
        for (let i = 0; i < this.selectedRowKeys.length; i++) {
          let selectedRowKey = this.selectedRowKeys[i]
            this.selectId.push(selectedRowKey)
        }
        let param = {
          recordId:this.selectId
        }
        postAction(this.url.doLoads, param).then((res) => {
          let url="";
          for (let i = 0; i < res.result.length; i++) {
            let fileKks = res.result[i]
            // window.open(window._CONFIG['domianURL']+"/"+fileKks.fileKk);
            url=window._CONFIG['domianURL']+"/"+fileKks.fileKk
            const iframe = document.createElement("iframe");
            iframe.style.display = "none"; // 防止影响页面
            iframe.style.height = 0; // 防止影响页面
            iframe.src = url;
            document.body.appendChild(iframe); // 这一行必须，iframe挂在到dom树上才会发请求
            // 5分钟之后删除（onload方法对于下载链接不起作用，就先抠脚一下吧）
            setTimeout(()=>{
              iframe.remove();
            }, 5 * 60 * 1000);
          }
        })
      },
      DownLoadClick(docName, recordId) {
        handleExportDoc(docName, recordId)
      },
      batchStartDate(record, operation) {
        this.ids = []
        this.ids.push(record.id)
        this.$refs.dateForm.edit()
        if (operation == 1) {
          this.$refs.dateForm.batchEdit(this.ids, '1')
        } else {
          this.$refs.dateForm.batchEdit(this.selectedRowKeys, '1')
        }
        this.$refs.dateForm.title = '修改生效日期'
        this.$refs.dateForm.disableSubmit = false
        this.$refs.dateForm.width = 500
      },
      batchEndDate(record, operation) {
        this.ids = []
        this.ids.push(record.id)
        this.$refs.dateForm.edit()
        if (operation == 1) {
          this.$refs.dateForm.batchEdit(this.ids, '2')
        } else {
          this.$refs.dateForm.batchEdit(this.selectedRowKeys, '2')
        }
        this.$refs.dateForm.title = '修改到期日期'
        this.$refs.dateForm.disableSubmit = false
        this.$refs.dateForm.width = 500
      },
      batchRecord() {
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！')
          return false
        } else if (this.selectedRowKeys.length > 1) {
          this.$message.warning('不支持查看多条记录')
        } else {
          this.$refs.recordForm.edit()
          this.$refs.recordForm.title = '操作记录'
          this.$refs.recordForm.disableSubmit = false
          let id = this.selectedRowKeys[0]
          this.$refs.recordForm.open(id)
        }
      },
      handleRecord: function (record) {
        this.$refs.recordForm.edit()
        this.$refs.recordForm.title = '操作记录'
        this.$refs.recordForm.disableSubmit = false
        let id = record.id
        this.$refs.recordForm.open(id)
      },
      initDictConfig() {
      }
      ,
      callback(key) {
        console.log(key)
      }
      ,
      loadCheck(arg) {
        this.dataSource = []
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = {
          jic: this.jic,
          docName: this.docName,
          docStatus: this.docStatus,
          docExpired: this.docExpired
        };//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      loadData() {
        this.refresh()
      },
      loadTree() {
        var that = this
        that.treeData = []
        that.categoryTree = []
        queryCategoryTreeList().then((res) => {
          if (res.success && res.result) {
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.categoryTree.push(temp)
              that.setThisExpandedKeys(temp)
            }
            this.loading = false
          }
          that.userIdentity = res.message
        })
      }
      ,
      setThisExpandedKeys(node) {
        if (node.children && node.children.length > 0) {
          this.iExpandedKeys.push(node.key)
          for (let a = 0; a < node.children.length; a++) {
            this.setThisExpandedKeys(node.children[a])
          }
        }
      }
      ,
      refresh() {
        this.loading = true
        this.loadTree()
      },
      sx() {
        this.onSelect(this.selectedKeys,this.event)
      }
      ,
      onSelect(selectedKeys, e) {
        this.event = e
        if (this.selectedKeys[0] !== selectedKeys[0]) {
          this.selectedKeys = [selectedKeys[0]]
        }
        let record = e.node.dataRef
        this.dataSource = []
        var formData = {
          docCategory: record.id,
        }
        this.id = record.id
        getAction(this.url.list, formData, 'get').then((res) => {
          if (res.result.records != null && res.result.records != 'null' && res.result.records.length > 0) {
            for (let i = 0; i < res.result.records.length; i++) {
              let data = res.result.records[i]
              if (data.docCategory == this.id) {
                this.dataSource.push(data)
              }
            }
          }
          this.ipagination.total = this.dataSource.length;
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>